package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.itheima.dao.WebConfigMapper;
import com.itheima.pojo.WebConfig;
import com.itheima.pojo.WebConfigVO;
import com.itheima.service.WebConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class WebConfigServiceImpl implements WebConfigService {

    @Autowired
    private WebConfigMapper webConfigMapper;

    /**
     * 展示网站配置数据列表
     * @return
     */
    @Override
    public List<WebConfig> getWebConfig() {

        List<WebConfig> webConfigs = webConfigMapper.selectAll();
        WebConfig webConfig = webConfigs.get(0);
        HashMap map = new HashMap();

        return webConfigs;
    }

    /**
     * 修改网站配置
     * @param webConfigVO
     * @return
     */
    @Override
    @Transactional
    public int editWebConfig(WebConfigVO webConfigVO) {

        Example example = new Example(WebConfig.class);
        Example.Criteria criteria = example.createCriteria();
        if(webConfigVO.getUid()!=null&&!"".equals(webConfigVO.getUid())){
            criteria.andEqualTo("uid",webConfigVO.getUid());
            WebConfig webConfig = JSON.parseObject(JSON.toJSONString(webConfigVO), WebConfig.class);
            webConfig.setUpdateTime(new Date());
            int i = webConfigMapper.updateByExample(webConfig, example);
            return i;
        }
        return 0;
    }
}
